package visualization.graphkeymodel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.ArrayList;
import visualization.Visualization;
import visualization.components.ColorGenerator;
import visualization.components.ColorKey;
import visualization.components.TextBox;


@SuppressWarnings("serial")
/**
 * 
 * @author Ben Schwab
 * A super class to allow the construction of graphs with a title, graph, and color key.
 *
 */
public abstract class GraphKeyModel extends Visualization {
    private static final double MARGIN_TOP = .05;
    private static final double TITLE_HEIGHT = .1;
    private static final double MARGIN_BOTTOM = .05;
    private static final double MARGIN_LEFT = .05;
    private static final double MARGIN_RIGHT = .05;
    private static final double GRAPH_SIZE = .6;
    private static final double KEY_MARGIN = .025;
    private static final int TITLE_SCALER = 2;
    private Rectangle myGraphArea;
    private Rectangle myKeyArea;
    private Rectangle myTitleArea;
    private ColorKey myColorKey;
    private Color[] myColors;
    private TextBox myTitle;
    private ColorGenerator myColorGenerator;

    /**
     * 
     * @param preferredSize the preferred size of this visualization
     */
    public GraphKeyModel (Dimension preferredSize) {
        super(preferredSize);
    }

    /**
     * @param title new title of visualization
     */
    public void setTitle (String title) {
        myTitle.setText(title);
    }

    /**
     * calculate the areas for the graph, key, and title
     */
    public void calculateAreas () {
        Dimension myDimension = getDimension();
        int startX = (int) (myDimension.getWidth() * MARGIN_LEFT);
        int startY = (int) (myDimension.getHeight() * (MARGIN_TOP + TITLE_HEIGHT));
        int pieWidth = (int) (myDimension.getWidth() * GRAPH_SIZE - startX);
        int pieHeight = (int) (myDimension.getHeight() * (1 - MARGIN_BOTTOM) - startY);
        myGraphArea = new Rectangle(startX, startY, pieWidth, pieHeight);
        int keyStartX = (int) (myDimension.getWidth() * (MARGIN_LEFT + GRAPH_SIZE + KEY_MARGIN));
        int keyStartY = (int) startY;
        int keyWidth = (int) (myDimension.getWidth() * (1 - MARGIN_RIGHT) - keyStartX);
        int keyHeight = pieHeight;
        myKeyArea = new Rectangle(keyStartX, keyStartY, keyWidth, keyHeight);
        myTitleArea =
                new Rectangle((int) startX, (int) (myDimension.getHeight() * MARGIN_TOP),
                              pieWidth / TITLE_SCALER,
                              (int) (myDimension.getHeight() * TITLE_HEIGHT));
    }

    /**
     * 
     * @return returns the area of the graph
     */
    public Rectangle getGraphArea () {
        return myGraphArea;
    }

    /**
     * 
     * @return returns the area of the key
     */
    public Rectangle getKeyArea () {
        return myKeyArea;
    }

    /**
     * 
     * @return returns the area of the title
     */
    public Rectangle getTitleArea () {
        return myTitleArea;
    }

    /**
     * 
     * @return returns the color key
     */
    public ColorKey getColorKey () {
        return myColorKey;
    }

    /**
     * generates a color key
     * 
     * @param titles list of Strings in the key
     */
    public void generateColorKey (ArrayList<String> titles) {
        myColorKey = new ColorKey(getKeyArea(), titles, myColors);

    }

    /**
     * 
     * @return returns the colors of this visualization
     */
    public Color[] getColors () {
        return myColors;
    }

    /**
     * Creates a title for a visualizaiton
     */
    public void generateTitle () {
        myTitle = new TextBox(getTitleArea(), "test title");
    }

    /**
     * 
     * @return
     */
    public ColorGenerator getColorGenerator () {
        return myColorGenerator;
    }

    /**
     * 
     * @param size number of entries in the visualization
     */
    public void createNewColorGenerator (int size) {
        myColorGenerator = new ColorGenerator(size);
    }

    /**
     * generate random colors for the visualization
     */
    public void generateRandomColors () {
        myColors = myColorGenerator.generateRandomColors();
    }

    /**
     * 
     * @return returns the title of the visualization
     */
    public TextBox getTitle () {
        return myTitle;
    }

}
